#include "aerial\Object\Panel.h"

#include "aerial\Scene\SceneManager.h"

using namespace air;

Panel::Panel(hgeResourceManager* hgeResMan ,float x, float y, float w, float h): IGuiObject(hgeResMan)
{
	width = w;
	height = h;

	status = 0;

	flashw = 0.0f;

	frame = new hgeSprite(0, 0, 0, w, h);
	flash = new hgeSprite(0, 0, 0, 0, h);

	frame->SetHotSpot(0.0f, 0.0f);
	flash->SetHotSpot(0.0f, 0.0f);

	frame->SetColor(0x1f0000ff);
	flash->SetColor(0x2fffffff); 
	ox = -w;
	oy = 0;
	rect.Set(0, 0, w, h);
	
}

void Panel::Update(float dt)
{
	if (status == 1)
	{
		moveTo(ox + width * 3 * dt, oy);
		//rect.x1 += frame->GetWidth() * 3 * dt;
		//rect.x2 = rect.x1 + frame->GetWidth();
	} 
	else if (status == 2)
	{
		moveTo(ox - width * 3 * dt, oy);
		/*rect.x1 -= frame->GetWidth() * 3 *dt;
		rect.x2 = rect.x1 + frame->GetWidth();*/
	}
	else
	{
		if (ox < 0) moveTo(-width, oy);
		else moveTo(0, oy);
		/*if (rect.x1 < 0) rect.x1 = -frame->GetWidth();
		else rect.x1 = 0;
		rect.x2 = rect.x1 + frame->GetWidth();*/
	}
	flashw += frame->GetWidth() * dt;
	if (flashw > width) flashw = 0;
	isPositionUpdate = true;
}

void Panel::Render()
{
	calcAbsolutePosition();
	frame->Render(rect.x1, rect.y1);
	flash->RenderStretch(rect.x1, rect.y1, flashw, height);
}
/*
void Panel::moveTo(float x, float y)
{
	//Node<IGuiObject*>* p = container.Head()->next;
	//Node<IGuiObject*>* t = container.Tail();
	//TODO why cannot directly access layoutType
	//while (p != t)
	//{
	//	if (p->val->getLayoutType() == 0)
	//		p->val->moveTo(rect.x1 + p->val->getHorizontalOffset(), 
	//			rect.y1 + p->val->getVerticalOffset());
	//	else {
	//		float ox = p->val->getHorizontalOffset();
	//		float oy = p->val->getVerticalOffset();
	//		p->val->moveTo(rect.x1 * (1 - ox) + rect.x2 * ox,
	//			rect.y1 * (1 - oy) + rect.y2 * oy);
	//	}
	//	p = p->next;
	//}

	IGuiObject::moveTo(x, y);
}*/

bool Panel::IsDone()
{
	if (status == 1)
	{
		if (ox > 0) 
		{
			status = 0;
			return true;
		} else return false;
	}
	else if (status == 2)
	{
		if (ox < -frame->GetWidth())
		{
			status = 0;
			return false;
		} else return false;
	} 
	else return true;
}

void Panel::placeGuiObjectProportional(IGuiObject* iGuiObj, float px, float py)
{
	iGuiObj->setRelativeLayout();
	iGuiObj->setLayoutOffset(px, py);
	container.append(iGuiObj);
	/*iGuiObj->moveTo(px * rect.x2 + (1 - px) * rect.x1, 
		py * rect.y2 + (1 - py) * rect.y1);*/
}

void Panel::placeGuiObjectOffset(IGuiObject* iGuiObj, float x, float y)
{
	iGuiObj->setAbsoluteLayout();
	iGuiObj->setLayoutOffset(x, y);
	container.append(iGuiObj);
	//iGuiObj->moveTo(rect.x1 + x, rect.y1 + y);
}

bool Panel::addToGui(SceneManager* sceneMan, IGuiObject* _anchor)
{
	anchor = _anchor;
	sceneMan->addGuiObject(this);
	Node<IGuiObject*>* p = container.Head()->next;
	Node<IGuiObject*>* t = container.Tail();
	while (p != t)
	{
		p->val->addToGui(sceneMan, this);
		p = p->next;
	}
	return true;
}


